import { Component, OnInit, OnDestroy } from '@angular/core';
import { Subscription } from 'rxjs';
import { DashBrandComponentService } from './brand.component.service';
import { AppSettings } from "../../../../app.settings";

import { Scope } from '../../../../utils/emit';

import { Util } from '../../../../utils/util';

@Component({
  selector: 'dashborad-brand',
  templateUrl: './brand.component.html',
  providers: [DashBrandComponentService]
})
export class DashboradBrand {

  busy: Subscription;


  vm: any = {
    date: '',
    user: {}
  };

  brands: Array<any> = [];

  // 类目 接收pie component cat change
  cat: any = {
    catName: "",
    wholeName: "",
    categoryId: ""
  };

  // 参数
  param: any = {
    "rows": 15,
    "start": 0,
    "categoryId": "",
    "sort": 'sellMoney',
    "sortType": 'desc'
  };

  // 列名
  public columns: Array<any> = [
    {
      title: '品牌名',
      name: 'brandName',
      sort: false
    },
    {
      title: '日销售额',
      name: 'sellMoney',
      sort: true
    },
    {
      title: '日销售量',
      name: 'sellAmount',
      sort: true
    },
    {
      title: '近30天销售额',
      name: 'monthSellMoney',
      sort: true
    },
    {
      title: '近30天销售量',
      name: 'monthSellAmount',
      sort: true
    }
  ];
  sellEvent: Subscription;

  constructor(private _service: DashBrandComponentService, private appSettings: AppSettings, private util: Util, private scope: Scope) {


  }

  currentValue: any;

  ngOnInit() {

    // 如果不是第一次进入
    this.cat = this.appSettings.getAppCat();

    if (this.cat.categoryId != '') {
      this.http(this.param);
    }

    this.sellEvent = this.scope.on('dashborad-all-cur-cat', (data) => {

      console.log("dash brand");

      console.log(data);
      // this.cat = data.data;

      // alert(0);

      let _cat = data.data;

      this.cat = data.data;

      this.http(this.param);
    });
  }

  ngOnDestroy() {
    if (this.busy) {
      this.busy.unsubscribe();
    }

    if (this.sellEvent) {
      this.sellEvent.unsubscribe();
    }

  }

  // st-sort directive emit
  sortChanged(sort) {
    console.log('xxx');
    console.log(sort);
    this.http(this.param);
  }

  export() {

    console.log("export");
    let dim = ['id', 'date', 'categoryId', 'categoryName', 'categoryLevel', 'brandName', 'sellMoney', 'sellAmount', 'monthSellMoney', 'monthSellAmount', 'productNum', 'shopNum', 'tsellMoney', 'tsellAmount', 'monthTSellMoney', 'monthTSellAmount', 'tproductNum', 'tshopNum', 'thksellMoney', 'thksellAmount', 'monthThkSellMoney', 'monthThkSellAmount', 'thkproductNum', 'thkshopNum', 'gsellMoney', 'gsellAmount', 'monthGSellMoney', 'monthGSellAmount', 'gproductNum', 'gshopNum', 'csellMoney', 'csellAmount', 'monthCSellMoney', 'monthCSellAmount', 'cproductNum', 'cshopNum'];

    let dimText = ['id', '时间', '类目id', '类目', '类目等级', '品牌名', '日销售额', '日销售量', '近30天销售额', '近30天销售量', '动销宝贝数', '动销店铺数', '天猫日销售额', '天猫日销售量', '天猫近30天销售额', '天猫近30天销售量', '天猫宝贝数', '天猫店铺数', '天猫国际日销售额', '天猫国际日销售量', '天猫国际近30天销售额', '天猫国际近30天销售量', '天猫国际宝贝数', '天猫国际店铺数', '全球购日销售额', '全球购日销售量', '全球购近30天销售额', '全球购近30天销售量', '全球购宝贝数', '全球购店铺数', '淘宝日销售额', '淘宝日销售量', '淘宝近30天销售额', '淘宝近30天销售量', '淘宝宝贝数', '淘宝店铺数'];

    let data = this.util.jsonToArrry(this.brands, dim, dimText);


    this.util.export(data, this.cat.catName + "类目品牌排行榜");
  }

  http(param) {
    this.brands = [];

    this.brands.length = 0;

    this.param.categoryId = this.cat.categoryId;

    var res ={"code":0,"msg":"成功","data":{"value":[{"id":"50019780\u0001apple/苹果","date":"2017-12-03 00:00:00","categoryId":"50019780","categoryName":"数码/手机/家电--平板电脑/MID","categoryLevel":2,"brandName":"apple/苹果","sellAmount":1460,"tsellAmount":556,"csellAmount":803,"thksellAmount":0,"gsellAmount":101,"monthSellAmount":80295,"monthTSellAmount":53435,"monthCSellAmount":24765,"monthThkSellAmount":0,"monthGSellAmount":2095,"sellMoney":3700308.36,"tsellMoney":1469652.0,"csellMoney":1992564.3,"thksellMoney":0.0,"gsellMoney":238092.06,"monthSellMoney":2.1000236347E8,"monthTSellMoney":1.40849367E8,"monthCSellMoney":6.38692153E7,"monthThkSellMoney":0.0,"monthGSellMoney":5283781.17,"productNum":578,"tproductNum":29,"cproductNum":527,"thkproductNum":0,"gproductNum":22,"shopNum":224,"tshopNum":10,"cshopNum":204,"thkshopNum":0,"gshopNum":10},{"id":"50019780\u0001huawei/华为","date":"2017-12-03 00:00:00","categoryId":"50019780","categoryName":"数码/手机/家电--平板电脑/MID","categoryLevel":2,"brandName":"huawei/华为","sellAmount":590,"tsellAmount":474,"csellAmount":116,"thksellAmount":0,"gsellAmount":0,"monthSellAmount":36602,"monthTSellAmount":33635,"monthCSellAmount":2967,"monthThkSellAmount":0,"monthGSellAmount":0,"sellMoney":957773.0,"tsellMoney":768751.0,"csellMoney":189022.0,"thksellMoney":0.0,"gsellMoney":0.0,"monthSellMoney":5.994276212E7,"monthTSellMoney":5.5158691E7,"monthCSellMoney":4784071.12,"monthThkSellMoney":0.0,"monthGSellMoney":0.0,"productNum":330,"tproductNum":180,"cproductNum":150,"thkproductNum":0,"gproductNum":0,"shopNum":117,"tshopNum":48,"cshopNum":69,"thkshopNum":0,"gshopNum":0},{"id":"50019780\u0001microsoft/微软","date":"2017-12-03 00:00:00","categoryId":"50019780","categoryName":"数码/手机/家电--平板电脑/MID","categoryLevel":2,"brandName":"microsoft/微软","sellAmount":174,"tsellAmount":26,"csellAmount":148,"thksellAmount":0,"gsellAmount":0,"monthSellAmount":8470,"monthTSellAmount":5119,"monthCSellAmount":3351,"monthThkSellAmount":0,"monthGSellAmount":0,"sellMoney":806231.99,"tsellMoney":185488.03,"csellMoney":620743.96,"thksellMoney":0.0,"gsellMoney":0.0,"monthSellMoney":4.911424414E7,"monthTSellMoney":3.623250819E7,"monthCSellMoney":1.288173595E7,"monthThkSellMoney":0.0,"monthGSellMoney":0.0,"productNum":173,"tproductNum":64,"cproductNum":109,"thkproductNum":0,"gproductNum":0,"shopNum":69,"tshopNum":12,"cshopNum":57,"thkshopNum":0,"gshopNum":0},{"id":"50019780\u0001荣耀","date":"2017-12-03 00:00:00","categoryId":"50019780","categoryName":"数码/手机/家电--平板电脑/MID","categoryLevel":2,"brandName":"荣耀","sellAmount":363,"tsellAmount":336,"csellAmount":27,"thksellAmount":0,"gsellAmount":0,"monthSellAmount":23907,"monthTSellAmount":23358,"monthCSellAmount":549,"monthThkSellAmount":0,"monthGSellAmount":0,"sellMoney":352440.0,"tsellMoney":323222.0,"csellMoney":29218.0,"thksellMoney":0.0,"gsellMoney":0.0,"monthSellMoney":2.22561422E7,"monthTSellMoney":2.1583471E7,"monthCSellMoney":672671.2,"monthThkSellMoney":0.0,"monthGSellMoney":0.0,"productNum":160,"tproductNum":120,"cproductNum":40,"thkproductNum":0,"gproductNum":0,"shopNum":62,"tshopNum":37,"cshopNum":25,"thkshopNum":0,"gshopNum":0},{"id":"50019780\u0001jumper/中柏","date":"2017-12-03 00:00:00","categoryId":"50019780","categoryName":"数码/手机/家电--平板电脑/MID","categoryLevel":2,"brandName":"jumper/中柏","sellAmount":245,"tsellAmount":238,"csellAmount":7,"thksellAmount":0,"gsellAmount":0,"monthSellAmount":4167,"monthTSellAmount":3956,"monthCSellAmount":211,"monthThkSellAmount":0,"monthGSellAmount":0,"sellMoney":310515.0,"tsellMoney":302272.0,"csellMoney":8243.0,"thksellMoney":0.0,"gsellMoney":0.0,"monthSellMoney":4513104.2,"monthTSellMoney":4293803.0,"monthCSellMoney":219301.2,"monthThkSellMoney":0.0,"monthGSellMoney":0.0,"productNum":67,"tproductNum":46,"cproductNum":21,"thkproductNum":0,"gproductNum":0,"shopNum":9,"tshopNum":5,"cshopNum":4,"thkshopNum":0,"gshopNum":0},{"id":"50019780\u0001teclast/台电","date":"2017-12-03 00:00:00","categoryId":"50019780","categoryName":"数码/手机/家电--平板电脑/MID","categoryLevel":2,"brandName":"teclast/台电","sellAmount":179,"tsellAmount":165,"csellAmount":14,"thksellAmount":0,"gsellAmount":0,"monthSellAmount":9501,"monthTSellAmount":8908,"monthCSellAmount":593,"monthThkSellAmount":0,"monthGSellAmount":0,"sellMoney":203598.0,"tsellMoney":184813.0,"csellMoney":18785.0,"thksellMoney":0.0,"gsellMoney":0.0,"monthSellMoney":9980426.98,"monthTSellMoney":9321778.0,"monthCSellMoney":658648.98,"monthThkSellMoney":0.0,"monthGSellMoney":0.0,"productNum":241,"tproductNum":99,"cproductNum":142,"thkproductNum":0,"gproductNum":0,"shopNum":34,"tshopNum":11,"cshopNum":23,"thkshopNum":0,"gshopNum":0},{"id":"50019780\u0001fitu","date":"2017-12-03 00:00:00","categoryId":"50019780","categoryName":"数码/手机/家电--平板电脑/MID","categoryLevel":2,"brandName":"fitu","sellAmount":378,"tsellAmount":378,"csellAmount":0,"thksellAmount":0,"gsellAmount":0,"monthSellAmount":11079,"monthTSellAmount":11079,"monthCSellAmount":0,"monthThkSellAmount":0,"monthGSellAmount":0,"sellMoney":169004.0,"tsellMoney":169004.0,"csellMoney":0.0,"thksellMoney":0.0,"gsellMoney":0.0,"monthSellMoney":5007062.0,"monthTSellMoney":5007062.0,"monthCSellMoney":0.0,"monthThkSellMoney":0.0,"monthGSellMoney":0.0,"productNum":3,"tproductNum":3,"cproductNum":0,"thkproductNum":0,"gproductNum":0,"shopNum":1,"tshopNum":1,"cshopNum":0,"thkshopNum":0,"gshopNum":0},{"id":"50019780\u0001麦柴","date":"2017-12-03 00:00:00","categoryId":"50019780","categoryName":"数码/手机/家电--平板电脑/MID","categoryLevel":2,"brandName":"麦柴","sellAmount":406,"tsellAmount":406,"csellAmount":0,"thksellAmount":0,"gsellAmount":0,"monthSellAmount":10812,"monthTSellAmount":10812,"monthCSellAmount":0,"monthThkSellAmount":0,"monthGSellAmount":0,"sellMoney":157468.0,"tsellMoney":157468.0,"csellMoney":0.0,"thksellMoney":0.0,"gsellMoney":0.0,"monthSellMoney":4542896.0,"monthTSellMoney":4542896.0,"monthCSellMoney":0.0,"monthThkSellMoney":0.0,"monthGSellMoney":0.0,"productNum":4,"tproductNum":4,"cproductNum":0,"thkproductNum":0,"gproductNum":0,"shopNum":1,"tshopNum":1,"cshopNum":0,"thkshopNum":0,"gshopNum":0},{"id":"50019780\u0001ssa","date":"2017-12-03 00:00:00","categoryId":"50019780","categoryName":"数码/手机/家电--平板电脑/MID","categoryLevel":2,"brandName":"ssa","sellAmount":267,"tsellAmount":267,"csellAmount":0,"thksellAmount":0,"gsellAmount":0,"monthSellAmount":5833,"monthTSellAmount":5833,"monthCSellAmount":0,"monthThkSellAmount":0,"monthGSellAmount":0,"sellMoney":114333.0,"tsellMoney":114333.0,"csellMoney":0.0,"thksellMoney":0.0,"gsellMoney":0.0,"monthSellMoney":2565574.0,"monthTSellMoney":2565574.0,"monthCSellMoney":0.0,"monthThkSellMoney":0.0,"monthGSellMoney":0.0,"productNum":9,"tproductNum":9,"cproductNum":0,"thkproductNum":0,"gproductNum":0,"shopNum":1,"tshopNum":1,"cshopNum":0,"thkshopNum":0,"gshopNum":0},{"id":"50019780\u0001酷比魔方","date":"2017-12-03 00:00:00","categoryId":"50019780","categoryName":"数码/手机/家电--平板电脑/MID","categoryLevel":2,"brandName":"酷比魔方","sellAmount":144,"tsellAmount":104,"csellAmount":40,"thksellAmount":0,"gsellAmount":0,"monthSellAmount":3594,"monthTSellAmount":3214,"monthCSellAmount":380,"monthThkSellAmount":0,"monthGSellAmount":0,"sellMoney":102238.0,"tsellMoney":74136.0,"csellMoney":28102.0,"thksellMoney":0.0,"gsellMoney":0.0,"monthSellMoney":3191749.4,"monthTSellMoney":2908206.0,"monthCSellMoney":283543.4,"monthThkSellMoney":0.0,"monthGSellMoney":0.0,"productNum":112,"tproductNum":49,"cproductNum":63,"thkproductNum":0,"gproductNum":0,"shopNum":21,"tshopNum":7,"cshopNum":14,"thkshopNum":0,"gshopNum":0},{"id":"50019780\u0001samsung/三星","date":"2017-12-03 00:00:00","categoryId":"50019780","categoryName":"数码/手机/家电--平板电脑/MID","categoryLevel":2,"brandName":"samsung/三星","sellAmount":40,"tsellAmount":19,"csellAmount":20,"thksellAmount":0,"gsellAmount":1,"monthSellAmount":1627,"monthTSellAmount":1001,"monthCSellAmount":612,"monthThkSellAmount":0,"monthGSellAmount":14,"sellMoney":86361.0,"tsellMoney":37939.0,"csellMoney":45123.0,"thksellMoney":0.0,"gsellMoney":3299.0,"monthSellMoney":3049173.7,"monthTSellMoney":1865108.0,"monthCSellMoney":1137479.7,"monthThkSellMoney":0.0,"monthGSellMoney":46586.0,"productNum":207,"tproductNum":97,"cproductNum":108,"thkproductNum":0,"gproductNum":2,"shopNum":39,"tshopNum":12,"cshopNum":26,"thkshopNum":0,"gshopNum":1},{"id":"50019780\u0001lenovo/联想","date":"2017-12-03 00:00:00","categoryId":"50019780","categoryName":"数码/手机/家电--平板电脑/MID","categoryLevel":2,"brandName":"lenovo/联想","sellAmount":59,"tsellAmount":25,"csellAmount":34,"thksellAmount":0,"gsellAmount":0,"monthSellAmount":2977,"monthTSellAmount":1969,"monthCSellAmount":1008,"monthThkSellAmount":0,"monthGSellAmount":0,"sellMoney":79759.97,"tsellMoney":32082.97,"csellMoney":47677.0,"thksellMoney":0.0,"gsellMoney":0.0,"monthSellMoney":3532527.51,"monthTSellMoney":2227503.51,"monthCSellMoney":1305024.0,"monthThkSellMoney":0.0,"monthGSellMoney":0.0,"productNum":199,"tproductNum":62,"cproductNum":137,"thkproductNum":0,"gproductNum":0,"shopNum":41,"tshopNum":9,"cshopNum":32,"thkshopNum":0,"gshopNum":0},{"id":"50019780\u0001more case for","date":"2017-12-03 00:00:00","categoryId":"50019780","categoryName":"数码/手机/家电--平板电脑/MID","categoryLevel":2,"brandName":"more case for","sellAmount":164,"tsellAmount":164,"csellAmount":0,"thksellAmount":0,"gsellAmount":0,"monthSellAmount":6246,"monthTSellAmount":6246,"monthCSellAmount":0,"monthThkSellAmount":0,"monthGSellAmount":0,"sellMoney":72682.0,"tsellMoney":72682.0,"csellMoney":0.0,"thksellMoney":0.0,"gsellMoney":0.0,"monthSellMoney":2764928.0,"monthTSellMoney":2764928.0,"monthCSellMoney":0.0,"monthThkSellMoney":0.0,"monthGSellMoney":0.0,"productNum":4,"tproductNum":4,"cproductNum":0,"thkproductNum":0,"gproductNum":0,"shopNum":1,"tshopNum":1,"cshopNum":0,"thkshopNum":0,"gshopNum":0},{"id":"50019780\u0001品摄","date":"2017-12-03 00:00:00","categoryId":"50019780","categoryName":"数码/手机/家电--平板电脑/MID","categoryLevel":2,"brandName":"品摄","sellAmount":129,"tsellAmount":129,"csellAmount":0,"thksellAmount":0,"gsellAmount":0,"monthSellAmount":5679,"monthTSellAmount":5679,"monthCSellAmount":0,"monthThkSellAmount":0,"monthGSellAmount":0,"sellMoney":64809.0,"tsellMoney":64809.0,"csellMoney":0.0,"thksellMoney":0.0,"gsellMoney":0.0,"monthSellMoney":2720295.0,"monthTSellMoney":2720295.0,"monthCSellMoney":0.0,"monthThkSellMoney":0.0,"monthGSellMoney":0.0,"productNum":3,"tproductNum":3,"cproductNum":0,"thkproductNum":0,"gproductNum":0,"shopNum":1,"tshopNum":1,"cshopNum":0,"thkshopNum":0,"gshopNum":0},{"id":"50019780\u0001艾伦","date":"2017-12-03 00:00:00","categoryId":"50019780","categoryName":"数码/手机/家电--平板电脑/MID","categoryLevel":2,"brandName":"艾伦","sellAmount":131,"tsellAmount":131,"csellAmount":0,"thksellAmount":0,"gsellAmount":0,"monthSellAmount":2771,"monthTSellAmount":2771,"monthCSellAmount":0,"monthThkSellAmount":0,"monthGSellAmount":0,"sellMoney":51876.0,"tsellMoney":51876.0,"csellMoney":0.0,"thksellMoney":0.0,"gsellMoney":0.0,"monthSellMoney":1097316.0,"monthTSellMoney":1097316.0,"monthCSellMoney":0.0,"monthThkSellMoney":0.0,"monthGSellMoney":0.0,"productNum":1,"tproductNum":1,"cproductNum":0,"thkproductNum":0,"gproductNum":0,"shopNum":1,"tshopNum":1,"cshopNum":0,"thkshopNum":0,"gshopNum":0}],"count":74}};

    

      // console.log(res);

      let data = res.data;

      this.brands = res.data.value;
      if (this.brands.length > 0) {
        this.vm.date = this.brands[0].date;
      }


    
  }
}
